
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"meun.h"

#define MAX_USERS 100
#define USERNAME_LEN 50
#define PASSWORD_LEN 50
void meun() {
    User users[MAX_USERS] = {0};
    int userCount = 0;

    char choice;
    do {
        printf("\n1. Register\n2. Login\n3. Exit\n");
        printf("Enter your choice: ");
        scanf("%c", &choice);

        switch (choice) {
            case '1':
                registerUser(users, &userCount);
                break;
            case '2':
                if (login(users, userCount)) {
                    printf("Login successful!\n");
                } else {
                    printf("Login failed!\n");
                }
                break;
            case '3':
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != '3');
for (size_t i = 0; i <userCount; i++)
{
    printf("username为%s,password%s\n",users[i].username,users[i].password);
}
    return ;
}

void registerUser(User *users, int *userCount) {
    if (*userCount >= MAX_USERS) {
        printf("User limit reached. Cannot register more users.\n");
        return;
    }

    User newUser;
    printf("Enter username: ");
    scanf("%49s", newUser.username);
    printf("Enter password: ");
    scanf("%49s", newUser.password);

    // 检查用户名是否已存在
    for (int i = 0; i < *userCount; i++) {
        if (strcmp(users[i].username, newUser.username) == 0) {
            printf("Username already exists.\n");
            return;
        }
    }

    users[*userCount] = newUser;
    (*userCount)++;
    printf("User registered successfully!\n");
}

int login(User *users, int userCount) {
    char username[USERNAME_LEN], password[PASSWORD_LEN];
    printf("Enter username: ");
    scanf("%49s", username);
    printf("Enter password: ");
    scanf("%49s", password);

    for (int i = 0; i < userCount; i++) {
        if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
            return 1; // 登录成功
        }
    }
    return 0; // 登录失败
}

